package com.itheima.exception;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result handleException(Exception ex) {

        log.error("服务器发生异常",ex);
        return Result.error("服务器发生异常");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException ex) {
        log.error("服务器发生异常",ex);
        String message = ex.getMessage();
        String[] s = message.split(" ");
        return Result.error("您输入的【"+s[2]+"】已经存在，请重新输入");
    }

}
